﻿using Factory.MasterData.Vendors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Polly;
using Factory.WarehouseManagement.NoticeOfArrivals.Dto;
using Redis.Cluster.Demo;
using Factory.Common; // 导入 LINQ 命名空间
using Newtonsoft.Json;

namespace Factory.WarehouseManagement.NoticeOfArrivals
{
    /// <summary>
    /// 实现到货通知业务逻辑
    /// </summary>
    public class NoticeOfArrivalAppService : IApplicationService, INoticeOfArrivalAppService
    {
        /// <summary>
        /// 到货通知仓储
        /// </summary>
        private readonly IRepository<NoticeOfArrival,int> _noticeOfArrivalRepository;

        /// <summary>
        /// 供应商仓储接口
        /// </summary>
        private readonly IRepository<Vendor, int> _vendorRepository;

        /// <summary>
        /// 物料信息
        /// </summary>
        private readonly IRepository<MaterielInfo, int> _materielInfoRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public NoticeOfArrivalAppService(IRepository<NoticeOfArrival, int> noticeOfArrivalRepository, IRepository<Vendor, int> vendorRepository, IRepository<MaterielInfo, int> materielInfoRepository)
        {
            _noticeOfArrivalRepository = noticeOfArrivalRepository;
            _vendorRepository=vendorRepository;
            _materielInfoRepository = materielInfoRepository;
        }

        /// <summary>
        /// 新增到货通知
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">页容量</param>
        /// <param name="notificationName">通知单名称</param>
        /// <returns>返回对应数据</returns>
        public async Task<PagedResult> PagedNoticeOfArrival(int page, int limit, string? notificationName)
        {
            var predicate = System.Linq.PredicateBuilder.New<NoticeOfArrival>(true);
            if (!string.IsNullOrEmpty(notificationName))
            {
                predicate = predicate.And(m => m.NotificationName.Contains(notificationName));
            }

            var noticeOfArrivalList = await _noticeOfArrivalRepository.GetListAsync(predicate);
            PagedResult pagedresult = new PagedResult();

            pagedresult.CurrentPage = noticeOfArrivalList.Count();
            pagedresult.Queryable = noticeOfArrivalList.Skip((page - 1) * limit).Take(limit).AsQueryable();

            return pagedresult;


        }


        /// <summary>
        /// 新增到货通知
        /// </summary>
        /// <param name="noticeOfArrival">到货通知实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(NoticeOfArrival noticeOfArrival)
        {
            var noticeOfArrivalList =await _noticeOfArrivalRepository.GetListAsync(m=>m.NotificationName== noticeOfArrival.NotificationName);
            if (noticeOfArrivalList.Count() > 0)
            {
                return -1;
            }
            await _noticeOfArrivalRepository.InsertAsync(noticeOfArrival);
            return 1;
        
        }



        private static Object obj = new Object();
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns></returns>
        public List<VendorSelectDto> GetVendor()
        {
            //根据Key值，获取学生list集合
            var getVendor = RedisClusterHelper.Get<List<VendorSelectDto>>("GetVendor");
            if (getVendor == null)
            {
                lock (obj)
                {
                    if (getVendor == null)
                    {
                        //从数据库中取出学生集合
                        var list = _vendorRepository.GetListAsync(x => x.IsNo == 1).Result;
                        getVendor = list.Select(x => new VendorSelectDto
                        {
                            Code = x.Code,
                            Name = x.Name,
                            VendorPhone = x.VendorPhone,
                            Contac = x.Contac

                        }).ToList();


                        //将学生集合缓存在Redis中，key值为：StudentList
                        RedisClusterHelper.Set("GetVendor", getVendor, DateTime.Now.AddDays(1));
                    }
                }
            }
            return getVendor;
        }


        /// <summary>
        /// 添加到货通知
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddCreate(NoticeOfArrival noticeOfArrival)
        {
           await _noticeOfArrivalRepository.InsertAsync(noticeOfArrival);
           return 1;
        }

        /// <summary>
        /// 到货通知详情
        /// </summary>
        /// <param name="notificationCode"></param>
        /// <returns></returns>
        public async Task<NoticeOfArrival> ArriveDetails(string notificationCode)
        {
            var notification = await _noticeOfArrivalRepository.FirstOrDefaultAsync(x=>x.NotificationCode== notificationCode);
            return notification;
        }


        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResult> MaterielInfoQuery(string notificationCode, int page, int limit)
        {
            //永远执行
            var materielInfo = System.Linq.PredicateBuilder.New<MaterielInfo>(true);
            materielInfo = materielInfo.And(m => m.NotificationCode == notificationCode);

            var pageList = await _materielInfoRepository.GetListAsync(materielInfo);

            PagedResult pagedResult = new PagedResult();

            pagedResult.CurrentPage = pageList.Count();
            pagedResult.Queryable = pageList.Skip((page - 1) * limit).Take(limit).AsQueryable();

            return pagedResult;
        }

        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> AddMaterielInfo(MaterielInfo materielInfo)
        {
            var addMaterielInfo = await _materielInfoRepository.InsertAsync(materielInfo);
            return 1;
        }





    }
}
